/**
 * created by 小卷毛, 2021-07-15
 * Copyright (c) 2021, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.rzico.sbl.viewmodel

import com.lzg.extend.BaseResponse
import com.lzg.extend.JsonConvert
import com.lzg.extend.jackson.JacksonConvert
import com.lzg.okrx.adapter.FlowableBody
import com.lzy.okgo.OkGo
import com.rzico.sbl.model.*
import com.rzico.sbl.ui.report.other.ReportUrl
import com.xinnuo.common.extend.ifNotEmpty
import com.xinnuo.common_ui.utils.applyFlowableSchedulers
import com.xinnuo.common_ui.utils.subscribeByFinally

/**
 * 描 述：人工补水操作相关
 * 作 者：小卷毛  2021-07-15 11:00
 * 修改描述：
 * 修 改 人：
 * 修改版本：
 */
class WaterViewModel : DialogViewModel() {

    /**
     * 企业列表
     */
    fun shopList(
        page: Int,
        keyWord: String,
        onSuccess: (ArrayList<WaterShopData>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.post<BaseResponse<ResponseModel<WaterShopData>>>(ReportUrl.getBaseUrl().waterShop)
            .isMultipart(true)
            .params(
                getParams(
                    "keyword" to keyWord,
                    "pageStart" to page * 20,
                    "pageSize" to 20
                )
            )
            .converter(object : JsonConvert<BaseResponse<ResponseModel<WaterShopData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 企业商品分类
     */
    fun productCat(shopId: String) =
        OkGo.get<BaseResponse<ArrayList<CommonData>>>(ReportUrl.getBaseUrl().productMCat)
            .params(getParams("id" to shopId))
            .converter(object : JacksonConvert<BaseResponse<ArrayList<CommonData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }
            .map { it.data }

    /**
     * 企业商品列表
     */
    fun goodsList(
        page: Int,
        shopId: String,
        kindId: String,
        keyWord: String,
        onSuccess: (ArrayList<ReportGoodsData>) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.post<BaseResponse<ResponseModel<ReportGoodsData>>>(ReportUrl.getBaseUrl().waterGoods)
            .isMultipart(true)
            .params(
                getParams(
                    "keyword" to keyWord,
                    "id" to shopId,
                    "productCategoryId" to kindId,
                    "pageStart" to page * 20,
                    "pageSize" to 20
                )
            )
            .converter(object : JsonConvert<BaseResponse<ResponseModel<ReportGoodsData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

    /**
     * 企业订单统计
     */
    fun shopSummary(
        page: Int,
        shopId: String,
        beginDate: String,
        endDate: String,
        onSuccess: (TotalModel?) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<ResponseBean<TotalModel>>>(ReportUrl.getBaseUrl().rptOrderSum)
            .params(
                getParams(
                    "id" to shopId,
                    "beginDate" to beginDate.ifNotEmpty { "$it 00:00:00" },
                    "endDate" to endDate.ifNotEmpty { "$it 23:59:59" },
                    "pageStart" to page * 20,
                    "pageSize" to 20
                )
            )
            .converter(object : JsonConvert<BaseResponse<ResponseBean<TotalModel>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data }
            .subscribeByFinally(
                event = this,
                onSuccess = { onSuccess(it.data) },
                onFinally = onFinally
            )
    )

    /**
     * 企业订单统计明细
     */
    fun shopDetail(
        page: Int,
        shopId: String,
        product: String,
        beginDate: String,
        endDate: String,
        onSuccess: (OrderModel) -> Unit = {},
        onFinally: () -> Unit = {}
    ) = mDisposables.add(
        OkGo.get<BaseResponse<OrderModel>>(ReportUrl.getBaseUrl().rptOrderDet)
            .params(
                getParams(
                    "id" to shopId,
                    "productId" to product,
                    "beginDate" to beginDate.ifNotEmpty { "$it 00:00:00" },
                    "endDate" to endDate.ifNotEmpty { "$it 23:59:59" },
                    "pageStart" to page * 20,
                    "pageSize" to 20
                )
            )
            .converter(object : JacksonConvert<BaseResponse<OrderModel>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data }
            .subscribeByFinally(
                event = this,
                onSuccess = onSuccess,
                onFinally = onFinally
            )
    )

}